package com.mayikt.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mayikt.entity.User;
import com.mayikt.entity.UserExample;
import com.mayikt.exception.BusinessException;
import com.mayikt.exception.BusinessExceptionCode;
import com.mayikt.mapper.UserMapper;
import com.mayikt.req.UserLoginReq;
import com.mayikt.req.UserQueryReq;
import com.mayikt.req.UserResetPasswordReq;
import com.mayikt.req.UserSaveReq;
import com.mayikt.resp.PageResp;
import com.mayikt.resp.UserLoginResp;
import com.mayikt.resp.UserQueryResp;
import com.mayikt.utils.CopyUtil;
import com.mayikt.utils.SnowFlake;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private SnowFlake snowFlake;

    /**
     * 查询
     * @param userQueryReq
     * @return
     */
    public PageResp<UserQueryResp> list(UserQueryReq userQueryReq) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();

        // 判断 lginName 条件是否为空
        if(!ObjectUtils.isEmpty(userQueryReq.getLoginName())) {
            criteria.andLoginNameLike("%" + userQueryReq.getLoginName() + "%");
        }

        // 分页
        PageHelper.startPage(userQueryReq.getPage(), userQueryReq.getSize());
        List<User> userList = userMapper.selectByExample(userExample);
        PageInfo<User> pageInfo = new PageInfo<>(userList);

        // do 转 dto
        List<UserQueryResp> userQueryResps = CopyUtil.copyList(userList, UserQueryResp.class);

        PageResp<UserQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(userQueryResps);

        return pageResp;
    }

    /**
     * 修改基本信息
     * @param userSaveReq
     * @return
     */
    @Transactional
    public int save(UserSaveReq userSaveReq) {
        User user = CopyUtil.copy(userSaveReq, User.class);
        int result = 0;
        if(ObjectUtils.isEmpty(userSaveReq.getId())) {
            User userDb = selectByLoginName(userSaveReq.getLoginName());
            if(ObjectUtils.isEmpty(userDb)) {
                // 雪花算法 生成id
                user.setId(snowFlake.nextId());
                // 新增
                result = userMapper.insert(user);
            } else {
                // 用户名已经存在
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            }
        } else {
            // 更新 根据是否有值来更新
            user.setLoginName(null);
            user.setPassword(null);
            return userMapper.updateByPrimaryKeySelective(user);
        }
        return result;
    }

    /**
     * 重置密码
     * @param req
     * @return
     */
    @Transactional
    public int resetPasswork(UserResetPasswordReq req) {
        User user = CopyUtil.copy(req, User.class);
        int result = userMapper.updateByPrimaryKeySelective(user);

        return result;
    }

    public User selectByLoginName(String loginName) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andLoginNameEqualTo(loginName);
        List<User> users = userMapper.selectByExample(userExample);
        if(CollectionUtils.isEmpty(users)) {
            return null;
        }
        return users.get(0);
    }

    @Transactional
    public int delete(Long id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    /**
     * 登录
     * @param userLoginReq
     * @return
     */
    public UserLoginResp login(UserLoginReq userLoginReq) {
        User userDb = selectByLoginName(userLoginReq.getLoginName());
        if(ObjectUtils.isEmpty(userDb)) {
            log.info("用户名不存在");
            throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
        } else {
            if(userDb.getPassword().equals(userLoginReq.getPassword())) {
                return CopyUtil.copy(userDb, UserLoginResp.class);
            } else {
                log.info("密码不正确");
                throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
            }
        }
    }
}
